package com.itheima.case2.service.impl;

import com.itheima.case2.dao.RoleMapper;
import com.itheima.case2.pojo.po.Role;
import com.itheima.case2.pojo.vo.AddRole;
import com.itheima.case2.pojo.vo.PageResult;
import com.itheima.case2.pojo.vo.QueryPageBean;
import com.itheima.case2.pojo.vo.UpdateRole;
import com.itheima.case2.service.RoleService;
import com.itheima.case2.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class RoleServiceImpl implements RoleService {
    //查询角色所有信息
    @Override
    public List<Role> queryAllRole() {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession(false);
        //获取接口代理对象
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        //调用查询方法
        List<Role> roles = mapper.queryAllRole();
        //释放资源
        session.close();
        return roles;
    }

    //分页查询角色和对应权限信息
    @Override
    public PageResult queryRoleAndPerByPage(QueryPageBean pageBean) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession(false);
        //获取接口代理对象
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        //获取起始索引和每页记录数   以及模糊查询条件
        Integer startIndex = pageBean.getOffset();
        Integer pageSize = pageBean.getPageSize();
        Map<String,String> queryParams = pageBean.getQueryParams();
        String name = queryParams.get("name");
        //调用对应方法
        List<Role> roles = mapper.queryRolePerByPage(startIndex, pageSize, name);
        Long total = mapper.queryAllRoleCount(name);
        //封装数据到PageResult中
        PageResult pageResult = new PageResult(total,roles);
        //释放资源
        session.close();
        return pageResult;
    }

    //修改角色和对应权限信息
    @Override
    public void updateRoleAndPer(UpdateRole updateRole) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        //调用方法
        mapper.updateRole(updateRole);
        //删除角色对应的权限
        mapper.deleteById(updateRole.getId().toString());
        //遍历添加角色对应的权限
        List<String> permissionIds = updateRole.getPermissionIds();
        for (String permissionId : permissionIds) {
            mapper.insertUserAndRole(updateRole.getId().toString(),permissionId);
        }
        //释放资源
        session.close();
    }

    //添加角色和对应权限信息
    @Override
    public void insertRoleAndPer(AddRole addRole) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        //调用方法
        mapper.insertRole(addRole);
        List<String> permissionIds = addRole.getPermissionIds();
        for (String permissionId : permissionIds) {
            mapper.insertUserAndRole(addRole.getId().toString(),permissionId);
        }
        //释放资源
        session.close();
    }

    //删除角色和对应权限信息
    @Override
    public void deleteById(String roleId) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        //调用方法
        mapper.deleteById(roleId);
        mapper.deleteRole(roleId);
        //释放资源
        session.close();

    }
}
